﻿using Power.View.Dialog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Power.View;

namespace Power.ViewModel.Dialog
{
    class DataUploadViewModel: ViewModelBase
    {
        private DataUpload model;
        private int progress;
        public int Progress { 
            get {
                return progress;
            }
            set {

                progress = value;
                ProgressWidth = 510 * Progress / 100.0m;
                ProgressDesc = progress + "%";
                RaisePropertyChanged();
            } }
        private decimal progressWidth;
        public decimal ProgressWidth { 
            get {
                return progressWidth;
            }
            set
            {

                progressWidth = value;
                RaisePropertyChanged();
            }
        }
        private string progressDesc;
        public string ProgressDesc {
            get
            {
                return progressDesc;
            }
            set
            {

                progressDesc = value;
                RaisePropertyChanged();
            }
        }
        public DataUploadViewModel(DataUpload model)
        {
            this.model = model;
            SetBtnOkEnable(false);
            Close = new RelayCommand(OnClose);
            SelectFile = new RelayCommand(OnSelectFile);
            CloseOK = new RelayCommand(OnCloseOK);
            UploadFile = new RelayCommand(OnUploadFile);
            
        }


        public void SetBtnOkEnable(bool status)
        {

            this.model.btnOk.Dispatcher.Invoke(() => {
                this.model.btnOk.IsEnabled = status;
            });

            this.model.btnOkBorder.Dispatcher.Invoke(() => {
                if (status == false)
                {
                    this.model.btnOkBorder.Background = new SolidColorBrush(Color.FromRgb(102,102,102));
                }
                else
                {
                    this.model.btnOkBorder.Background = new SolidColorBrush(Color.FromRgb(53,109,236));
                }
            });
            
        }
        private void OnClose()
        {
            this.model.DialogResult = false;
            this.model.Close();
        }
        //检测结果上传
        private void OnSelectFile()
        {
            System.Windows.Forms.FolderBrowserDialog openFileDialog = new System.Windows.Forms.FolderBrowserDialog();  //选择文件夹
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)//注意，此处一定要手动引入System.Window.Forms空间，否则你如果使用默认的DialogResult会发现没有OK属性
            {
                this.model.FileAt.Text = openFileDialog.SelectedPath;
            }
        }

        private void OnCloseOK()
        {
            this.model.DialogResult = true;
            this.model.Close();
        }
        private void OnUploadFile()
        {
            Task.Run(() =>
            {

                while (Progress<100)
                {
                    Progress++;
                    

                    Task.Delay(50).Wait();
                }

                SetBtnOkEnable(true);
               
                
            });
        }
        #region 
        public RelayCommand Close { get; private set; }

        public RelayCommand SelectFile { get; private set; }

        public RelayCommand CloseOK { get; private set; }
        public RelayCommand UploadFile { get; private set; }
        #endregion
    }
}
